package com.example.booktest1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;

public class ExternalStorageTest extends Activity {
	StringBuilder builder = new StringBuilder();
	TextView textView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textView = new TextView(this);
		setContentView(textView);

		String state = Environment.getExternalStorageState();
		if (!state.equals(Environment.MEDIA_MOUNTED)) {
			textView.setText("No external storage mounted");
		} else {
			File externalDir = Environment.getExternalStorageDirectory();
			File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
			try {
				writeTextFile(textFile, "This is a text. Roger");
				String text = readTextFile(textFile);
				textView.setText(text);
				if(!textFile.delete()) {
					textView.setText("Couldn't remove temporary file");
				}
			} catch (IOException e) {
				textView.setText("Something went wrong " + e.getMessage());
			}
		}
	}


	public void writeTextFile(File file, String text) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		writer.write(text);
		writer.close();
	}

	public String readTextFile(File file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		StringBuilder text = new StringBuilder();
		String line;
		while ((line = reader.readLine()) != null) {
			text.append(line);
			text.append("\n");
		}
		reader.close();
		return text.toString();
	}

//	public String readTextFileFromExternalStorage() throws IOException {
//		StringBuilder text = null;
//		String state = Environment.getExternalStorageState();
//		if (!state.equals(Environment.MEDIA_MOUNTED)) {
//			textView.setText("No external storage mounted");
//		} else {
//			File externalDir = Environment.getExternalStorageDirectory();
//			File textFile = new File(externalDir.getAbsolutePath() + File.separator + "mytextfile.txt");
//			BufferedReader reader = new BufferedReader(new FileReader(textFile));
//			text = new StringBuilder();
//			String line;
//			while ((line = reader.readLine()) != null) {
//				text.append(line);
//				text.append("\n");
//			}
//			reader.close();
//		}
//		return text.toString();
//	}


	//	public void writeTextFileToExternalStorage() throws IOException {
	//		String state = Environment.getExternalStorageState();
	//		if (!state.equals(Environment.MEDIA_MOUNTED)) {
	//			textView.setText("No external storage mounted");
	//		} else {
	//			File externalDir = Environment.getExternalStorageDirectory();
	//			File textFile = new File(externalDir.getAbsolutePath() + File.separator + "mytextfile.txt");
	//			try {
	//				BufferedWriter writer = new BufferedWriter(new FileWriter(textFile));
	//				writer.write("I love text files. I could eat a whole one");
	//				writer.close();
	//			} catch (IOException e) {
	//				Log.d("Something went wrong ", e.getMessage());
	//			}
	//		}
	//	}


}
